﻿Imports System.Net

Public Class TestForm

    Dim wh As New WebHelper

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Not String.IsNullOrEmpty(txtUrl.Text) Then
            wh.LoadWebPage(txtUrl.Text)
        End If

        'txtResult.Text = DisplayHtml()
        Dim str(3) As String



        'Dim q As String = "/html/body/div/center/table/tbody/tr[3]/td[2]/div/table/tbody/tr/td[1]/table[2]/tbody/tr[1]/td/div/table/tbody/tr/td[4]/div/table/tbody/tr[2]/td/table[1]/tbody/tr/td/font/b"
        'node = doc.DocumentNode.SelectSingleNode(q)

        'If node IsNot Nothing Then

        'End If


        For Each link In wh.HtmlDoc.DocumentNode.SelectNodes("//table")
            str(0) = link.InnerHtml.Trim()
            str(1) = link.InnerText.Trim()
            str(2) = link.XPath

            Dim lvi As New ListViewItem(str)
            lvLog.Items.Add(lvi)
        Next

    End Sub

    Private Function DisplayHtml() As String
        Dim ret As String = String.Empty

        Try
            Dim node As HtmlAgilityPack.HtmlNode

            node = wh.HtmlDoc.DocumentNode.SelectSingleNode(TextBox1.Text)

            If node IsNot Nothing Then
                If rbHtml.Checked Then
                    ret = node.InnerHtml
                ElseIf rbText.Checked Then
                    ret = node.InnerText
                End If
            Else
                ret = "Null"
            End If

        Catch ex As Exception
            ret = "Error in your expression:\r\n" + ex.Message
        End Try

        Return ret

    End Function


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, rbHtml.CheckedChanged, rbText.CheckedChanged
        txtResult.Text = DisplayHtml()
    End Sub

    Private Sub lvLog_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvLog.DoubleClick
        If Not lvLog.SelectedItems(0) Is Nothing Then
            Clipboard.SetText(lvLog.SelectedItems(0).SubItems(2).Text)
        End If
    End Sub

    Private Function DownloadImage(ByVal url As String, ByVal filename As String) As String

        Try
            Dim fileReader As New WebClient()

            If Not (System.IO.File.Exists(filename)) Then
                fileReader.DownloadFile(url, filename)
            End If
        Catch ex As HttpListenerException
            'MsgBox("Error accessing " + url + " - " + ex.Message)
            filename = String.Empty
        Catch ex As Exception
            'MsgBox("Error accessing " + url + " - " + ex.Message)
            filename = String.Empty
        End Try
        Return filename
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim img As String
        ProgressBar1.Minimum = 1
        ProgressBar1.Maximum = 600
        For i As Integer = 1 To 600
            Application.DoEvents()
            wh.LoadWebPage(String.Format(ConfigurationManager.AppSettings("ZagorWebSite"), i))
            img = wh.HtmlDoc.DocumentNode.SelectSingleNode(ConfigurationManager.AppSettings("FieldConstantImageZagor")).InnerHtml.Trim
            img = TextBetween(img, "('", "'")
            'DownloadImage(img, System.IO.Path.GetTempPath() + "Zagor\Zagor\Zagor" + i.ToString.PadLeft(4, "0") + ".jpg")
            DownloadImage(img, "G:\Temp\Zagor\Zagor" + i.ToString.PadLeft(4, "0") + ".jpg")
            ProgressBar1.Value = i
        Next

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'MultipleFileFilter("D:\Zagor\tmp", "*.cbz,*.cbr")
    End Sub
End Class